import { cascaderFilter, defaultRanges } from '../componentMap';
interface ConfiMater {
    _config_: { [key: string]: any };
    _props_: { [key: string]: any };
    _other_: { [key: string]: any };
    [key: string]: any,
}

// 默认指定浮层根据对话框内容浮动
const getPopupContainer = (triggerNode: Element) => {
    const modalBody = document.querySelector('.ant-modal-body');
    if(modalBody){
        return modalBody;
    }
    const inputBody = document.querySelector('.ant-form-item-control-input-content');
    if(inputBody){
        return inputBody;
    }
    return triggerNode;
};

export const useSchemas = (config: ConfiMater[]) => {
    return config.map((item:any) => {
        if(item?._config_?.component == 'Select'){
            item._props_.filterOption = (input: string, option: any) => {
                return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
            };
        }
        if(item?._config_?.component == 'Cascader'){
            item._props_.showSearch = { filter: cascaderFilter };
        }
        if(item?._config_?.component == 'RangePicker'){
            item._props_.ranges = defaultRanges();
        }
        const components = ['Select', 'Cascader', 'TreeSelect', 'TimePicker', 'DatePicker', 'MonthPicker', 'RangePicker'];
        if(components.includes(item?._config_?.component)){
            item._props_.getPopupContainer = getPopupContainer;
        }

        if(item?._config_?.colProps?.span){
            item._config_.colProps = { xs: 24, sm: 24, md: 24, lg: item?._config_?.colProps?.span, xl: item?._config_?.colProps?.span, xxl: item?._config_?.colProps?.span };
        }
        return {
            ...item?._config_,
            componentProps: item?._props_,
        };
    });
};